package com.ledim.db.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.ledim.bean.LedimPicBean;
import com.ledim.bean.LedimUserBean;

import com.ledim.bean.ChatRoomBean;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "ROOM".
*/
public class ChatRoomBeanDao extends AbstractDao<ChatRoomBean, Long> {

    public static final String TABLENAME = "ROOM";

    /**
     * Properties of entity ChatRoomBean.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "Id");
        public final static Property RoomId = new Property(1, String.class, "roomId", false, "room_id");
        public final static Property Name = new Property(2, String.class, "name", false, "name");
        public final static Property Intro = new Property(3, String.class, "intro", false, "intro");
        public final static Property Status = new Property(4, int.class, "status", false, "status");
        public final static Property CoverId = new Property(5, long.class, "coverId", false, "cover");
        public final static Property UserId = new Property(6, long.class, "userId", false, "user");
        public final static Property Created_at = new Property(7, String.class, "created_at", false, "created_at");
        public final static Property Time = new Property(8, long.class, "time", false, "time");
    };

    private DaoSession daoSession;


    public ChatRoomBeanDao(DaoConfig config) {
        super(config);
    }
    
    public ChatRoomBeanDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"ROOM\" (" + //
                "\"Id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"room_id\" TEXT UNIQUE ," + // 1: roomId
                "\"name\" TEXT," + // 2: name
                "\"intro\" TEXT," + // 3: intro
                "\"status\" INTEGER NOT NULL ," + // 4: status
                "\"cover\" INTEGER NOT NULL ," + // 5: coverId
                "\"user\" INTEGER NOT NULL ," + // 6: userId
                "\"created_at\" TEXT," + // 7: created_at
                "\"time\" INTEGER NOT NULL );"); // 8: time
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"ROOM\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, ChatRoomBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String roomId = entity.getRoomId();
        if (roomId != null) {
            stmt.bindString(2, roomId);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }
 
        String intro = entity.getIntro();
        if (intro != null) {
            stmt.bindString(4, intro);
        }
        stmt.bindLong(5, entity.getStatus());
        stmt.bindLong(6, entity.getCoverId());
        stmt.bindLong(7, entity.getUserId());
 
        String created_at = entity.getCreated_at();
        if (created_at != null) {
            stmt.bindString(8, created_at);
        }
        stmt.bindLong(9, entity.getTime());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, ChatRoomBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String roomId = entity.getRoomId();
        if (roomId != null) {
            stmt.bindString(2, roomId);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }
 
        String intro = entity.getIntro();
        if (intro != null) {
            stmt.bindString(4, intro);
        }
        stmt.bindLong(5, entity.getStatus());
        stmt.bindLong(6, entity.getCoverId());
        stmt.bindLong(7, entity.getUserId());
 
        String created_at = entity.getCreated_at();
        if (created_at != null) {
            stmt.bindString(8, created_at);
        }
        stmt.bindLong(9, entity.getTime());
    }

    @Override
    protected final void attachEntity(ChatRoomBean entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public ChatRoomBean readEntity(Cursor cursor, int offset) {
        ChatRoomBean entity = new ChatRoomBean( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // roomId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // name
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // intro
            cursor.getInt(offset + 4), // status
            cursor.getLong(offset + 5), // coverId
            cursor.getLong(offset + 6), // userId
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // created_at
            cursor.getLong(offset + 8) // time
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, ChatRoomBean entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setRoomId(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setIntro(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setStatus(cursor.getInt(offset + 4));
        entity.setCoverId(cursor.getLong(offset + 5));
        entity.setUserId(cursor.getLong(offset + 6));
        entity.setCreated_at(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setTime(cursor.getLong(offset + 8));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(ChatRoomBean entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(ChatRoomBean entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getLedimPicBeanDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getLedimUserBeanDao().getAllColumns());
            builder.append(" FROM ROOM T");
            builder.append(" LEFT JOIN PHOTO T0 ON T.\"cover\"=T0.\"Id\"");
            builder.append(" LEFT JOIN USER T1 ON T.\"user\"=T1.\"Id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected ChatRoomBean loadCurrentDeep(Cursor cursor, boolean lock) {
        ChatRoomBean entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        LedimPicBean cover = loadCurrentOther(daoSession.getLedimPicBeanDao(), cursor, offset);
         if(cover != null) {
            entity.setCover(cover);
        }
        offset += daoSession.getLedimPicBeanDao().getAllColumns().length;

        LedimUserBean user = loadCurrentOther(daoSession.getLedimUserBeanDao(), cursor, offset);
         if(user != null) {
            entity.setUser(user);
        }

        return entity;    
    }

    public ChatRoomBean loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<ChatRoomBean> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<ChatRoomBean> list = new ArrayList<ChatRoomBean>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<ChatRoomBean> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<ChatRoomBean> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
